//
//  RootViewController.m
//  much
//
//  Created by weijunjie on 2019/1/4.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "RootViewController.h"
#import "MallHomeViewController.h"
#import "CategoryHomeViewController.h"
#import "ReadHomeViewController.h"
#import "CartHomeViewController.h"
#import "UserCenterHomeViewController.h"
#import "RDVTabBarController.h"
#import "RDVTabBarItem.h"

@interface RootViewController ()
@property (strong, nonatomic, readonly) NSArray *tabBarConfigArray;

@end

@implementation RootViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //创建tabbar上的控制器
    NSMutableArray *controllers = [NSMutableArray array];
    NSMutableArray *normalImages = [NSMutableArray array];
    NSMutableArray *selectedImages = [NSMutableArray array];
    NSMutableArray *titles = [NSMutableArray array];
    [self.tabBarConfigArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DeepestViewController *viewController = [[NSClassFromString(obj[@"view_controller"]) alloc] init];
        EasyNavigationController *navigationController = [[EasyNavigationController alloc] initWithRootViewController:viewController];
        [controllers addObject:navigationController];
        [normalImages addObject:obj[@"icon_normal"]];
        [selectedImages addObject:obj[@"icon_selected"]];
        [titles addObject:obj[@"title"]];
    }];
    [self setViewControllers:controllers];
    self.tabBar.backgroundView.backgroundColor = [UIColor whiteColor];
    NSInteger index = 0;
    for (RDVTabBarItem *item in [[self tabBar] items]) {
        UIImage *unselectedImage = [UIImage imageNamed:normalImages[index]];
        UIImage *selectedImage = [UIImage imageNamed:selectedImages[index]];
        [item setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
        item.title = titles[index];
        item.selectedTitleAttributes = @{
                                         NSForegroundColorAttributeName : Color175030,
                                         NSFontAttributeName : APPFONT(12)
                                         };
        item.unselectedTitleAttributes = @{
                                           NSForegroundColorAttributeName : Color140,
                                           NSFontAttributeName : APPFONT(12)
                                           };
        item.titlePositionAdjustment = UIOffsetMake(1, 6);
        index++;
    }
    APP.rootViewController = self;
}

- (NSArray *)tabBarConfigArray {
    return @[
             @{@"title":@"商城",
               @"icon_normal":@"mall_1",
               @"icon_selected":@"mall_2",
               @"view_controller":NSStringFromClass([MallHomeViewController class])},
             @{@"title":@"分类",
               @"icon_normal":@"category_1",
               @"icon_selected":@"category_2",
               @"view_controller":NSStringFromClass([CategoryHomeViewController class])},
             @{@"title":@"文章",
               @"icon_normal":@"read_1",
               @"icon_selected":@"read_2",
               @"view_controller":NSStringFromClass([ReadHomeViewController class])},
             @{@"title":@"购物车",
               @"icon_normal":@"cart_1",
               @"icon_selected":@"cart_2",
               @"view_controller":NSStringFromClass([CartHomeViewController class])},
             @{@"title":@"我的",
               @"icon_normal":@"userCenter_1",
               @"icon_selected":@"userCenter_2",
               @"view_controller":NSStringFromClass([UserCenterHomeViewController class])}
             ];
}
@end
